<?PHP
/**
 * User: Paulo Fournier
 * Date: 05-09-2013
 * Type: application/json
 * Used to authenticate the user in the system
 */

require_once '../../../../php/settings/config.php';
require_once '../../../../php/lib/user.php';
require_once '../../../../php/lib/event.php';

session_start();

$success = FALSE; //used to check if the process was finished successfully
$errorCode = NULL; //in case of error, will contain the code error that will be return to the client

//init connection
$ch = curl_init(CRUD_URL . "/checkAuthentication/" . $_POST["username"] . "/" . $_POST["password"]);
//set connection paramaters
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "charset: UTF-8"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, FALSE);
//get response
$rst = curl_exec($ch);
$user = NULL;
$events = array();
//request failure
if ($rst === false) {
    $err = curl_error($ch);
    $errorCode = 2;

    error_log("request: NOK");
    error_log('Curl error: ' . $err);
} //request success
else {

    $response = json_decode($rst, TRUE);

    //auth successfully
    if ($response['success'] && $response["retVal"]["userId"]) {

        $success = TRUE;

        //set user object
        $user = new User(
            $response["retVal"] //user information it's returned on the [retVal] field
        );

        $_SESSION["loggedIn"] = TRUE; //flag that indicates if the session is authenticated
        $_SESSION["user"] = serialize($user); //save user object to the session

        curl_close($ch);

        //get event config options
        $ch = curl_init(CRUD_URL . "/eventConfiguration/" . $user->groupId);
        //set connection paramaters
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "charset: UTF-8"));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POST, FALSE);
        //get response
        $rst = curl_exec($ch);

        if ($rst === false) {
            $err = curl_error($ch);
            $errorCode = 4;

            error_log("request: NOK");
            error_log('Curl error: ' . $err);
        } else {
            $response = json_decode($rst, TRUE);
            

            foreach ($response["retVal"] as $i => $evt) {

                $events[$evt["eid"]] = new Event($evt);
            }

            $_SESSION["events"] = serialize($events); //save events object to the session
        }

    } //auth un-successfully
    else {
        $errorCode = 3;
    }

}

//print JSON result
echo json_encode(
    array(
        "success" => $success,
        "errorCode" => $errorCode,
        "user" => $user,
        "events" => array(
            "shape" => array(
                1 => '',
                2 => 'e-round',
                3 => 'e-flag-l',
                4 => 'e-flag-r',
                5 => 'e-flag-t',
                6 => 'e-flag-b',
                7 => 'e-flag-tl-br',
                8 => 'e-flag-tr-bl'
            ),
            "levels" => array(
                0 => array(
                    'css' => '',
                    'icon' => 'gray.png',
                    'color' => '#808080'
                ),
                1 => array(
                    'css' => 'e-bl',
                    'icon' => 'blue.png',
                    'color' => '#33cbf7'
                ),
                2 => array(
                    'css' => 'e-grn',
                    'icon' => 'green.png',
                    'color' => '#39c164'
                ),
                3 => array(
                    'css' => 'e-orng',
                    'icon' => 'orange.png',
                    'color' => '#ff9b29'
                ),
                4 => array(
                    'css' => 'e-rd',
                    'icon' => 'red.png',
                    'color' => '#e6462e'
                )
            ),
            "config" => $events
        )
    )
);